package com.neteast.androidclient.newscenter.domain;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Arrays;

public class LoginError {
	/** 通信协议 */
	public int protocolVersion;
	/** 报文id*/
	public int msgId;
	/** 错误编号*/
	public int errorId;
	/** 错误信息额外描述*/
	public String errorDesc;
	/** 保留字段长度*/
	public int reserveLen;
	/** 保留数据*/
	public byte[] reserve;
	
	private ByteBuffer byteBuffer;
	public LoginError(ByteBuffer byteBuffer) {
		this.byteBuffer=byteBuffer;
		init();
	}
	private void init() {
		protocolVersion=byteBuffer.getInt();
		msgId=byteBuffer.getInt();
		errorId=byteBuffer.getInt();
		byte[] error=new byte[20];
		for(int i=0;i<20;i++){
			error[i]=byteBuffer.get();
		}
		try {
			errorDesc=new String(error, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		reserveLen=byteBuffer.getInt();
		if (reserveLen>0) {
			reserve=new byte[reserveLen];
			for(int i=0;i<reserveLen;i++){
				reserve[i]=byteBuffer.get();
			}
		}
	}
	
	@Override
	public String toString() {
		if (reserveLen>0) {
			return "LoginError [protocolVersion=" + protocolVersion + ", msgId="
					+ msgId + ", errorId=" + errorId + ", errorDesc=" + errorDesc
					+ ", reserveLen=" + reserveLen + ", reserve="
					+ Arrays.toString(reserve) + "]";
		} else {
			return "LoginError [protocolVersion=" + protocolVersion + ", msgId="
					+ msgId + ", errorId=" + errorId + ", errorDesc=" + errorDesc
					+ ", reserveLen=" + reserveLen + "]";
		}
		
	}
}
